<template>
  <div class="app-container">
    <el-form
      ref="form"
      :inline="true"
      :model="formData"
      class="demo-form-inline"
    >
      <el-form-item label="客户名称">
        <el-input
          v-model="formData.name"
          placeholder="请输入客户名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="客户电话">
        <el-input
          v-model="formData.phone"
          placeholder="请输入客户电话"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-button class="create" type="primary" @click="create">创建</el-button>

    <el-table :data="list" style="width: 100%">
      <el-table-column prop="id" label="ID" width="180"> </el-table-column>
      <el-table-column prop="name" label="客户名称" width="120">
      </el-table-column>
      <el-table-column prop="phone" label="客户电话" width="120">
      </el-table-column>

      <el-table-column label="客户地址" width="150">
        <template slot-scope="scope">
          <span
            >{{ codeToText[scope.row.provinces]
            }}{{ codeToText[scope.row.country] }}{{ codeToText[scope.row.city]
            }}{{ scope.row.address }}</span
          >
        </template>
      </el-table-column>

      <el-table-column prop="orderNum" label="订单数量"> </el-table-column>

      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" type="success">正常</el-tag>
          <el-tag v-if="scope.row.status == 2" type="danger">禁用</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="remark" label="备注"> </el-table-column>
      <el-table-column prop="updateBy" label="操作人"> </el-table-column>
      <el-table-column prop="updateTime" label="更新时间" width="180">
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="edit(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="small"
            v-if="scope.row.status == 2"
            @click="updateStatus(scope.row, '1')"
            >启用</el-button
          >
          <el-button
            type="text"
            size="small"
            v-if="scope.row.status == 1"
            @click="updateStatus(scope.row, '2')"
            >禁用</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="queryParams.total > 0"
      :total="queryParams.total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.pageSize"
      @pagination="getListData"
    />
  </div>
</template>

<script>
import { getCustomerList, addOrEditCustomer } from "@/api/customer/index";
import { codeToText } from "element-china-area-data";
export default {
  name: "Index",
  data() {
    return {
      queryParams: {
        page: 0,
        pageSize: 10,
        total: 0,
      },
      formData: {
        name: "",
        phone: "",
      },
      list: [],
      codeToText,
    };
  },
  created() {
    this.getListData({
      page: 1,
      limit: 10,
    });
  },
  methods: {
    getListData({ page, limit }) {
      getCustomerList(
        this.queryParams,
        page || this.queryParams.page + 1,
        limit
      ).then(({ data }) => {
        this.list = data.records;
        this.queryParams.total = data.total;
        this.queryParams.page = data.current;
        this.queryParams.limit = data.size;
      });
    },
    onSearch() {
      this.queryParams = {
        ...this.queryParams,
        ...this.formData,
      };
      this.getListData({
        page: 1,
        limit: 10,
      });
    },
    reset() {
      this.formData = {
        name: "",
        phone: "",
      };
    },
    updateStatus(item, status) {
      addOrEditCustomer({
        ...item,
        status,
      }).then((res) => {
        this.loading = false;
        this.$message.success("操作成功");
        this.getListData(this.queryParams);
      });
    },
    create() {
      this.$router.push({ path: "/customer/edit" });
    },
    edit(id) {
      console.log(id, "zxcasd");
      this.$router.push({ path: `/customer/edit?id=${id}` });
    },
  },
};
</script>

<style lang="scss" scoped>
.create {
  margin-bottom: 20px;
}
</style>
